<?php

if(!$_SESSION) session_start();
// Connect to the database
////////////////////////////////////////////////////////

require_once("../scripts/config.php");

// Load Functions
////////////////////////////////////////////////////////

require_once("../scripts/functions.php");

// Insert Task
////////////////////////////////////////////////////////

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formVNT")) {

  $sql = sprintf("INSERT INTO tbltasks (UserID, CreatedbyID, ProjectID, Subject, date_due, set_priority, set_status, set_type, Body) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['UserID'], "int"),
                       GetSQLValueString($_SESSION['UserID'], "int"),
                       GetSQLValueString($_POST['ProjectID'], "int"),
                       GetSQLValueString($_POST['Subject'], "text"),
                       GetSQLValueString(date("Y-m-d",strtotime($_POST['date_due'])), "date"),
                       GetSQLValueString($_POST['set_priority'], "int"),
                       GetSQLValueString($_POST['set_status'], "int"),
                       GetSQLValueString($_POST['set_type'], "int"),
                       GetSQLValueString($_POST['Body'], "text"));

  $task = sqlMod($sql, $db);

  echo $sql;

   exit("Task Created Successfully!");

}

// Load Data
////////////////////////////////////////////////////////

$sql = "SELECT * FROM tblprojects WHERE project_label IS NOT NULL AND set_active = 1 ORDER BY project_label";

$projects = sqlLoad($sql, $db);

$sql = "SELECT * FROM tblusers WHERE set_active = 1 ORDER BY name_display";

$users = sqlLoad($sql, $db);

?>
<form id="formVNT" name="formVNT" method="POST" action="/forms/task_new.php">
  <table border="0" cellpadding="3" cellspacing="0">
    <tr>
      <td>Subject:</td>
      <td colspan="5"><input type="text" name="Subject" id="Subject" style="width:95%" /></td>
    </tr>
    <tr>
      <td nowrap="nowrap">Date Due:</td>
      <td><input name="date_due" type="text" id="date_due" value="<?php echo date("m/d/Y");?>" size="12" maxlength="12" /></td>
      <td>Status:</td>
      <td><select name="set_status" id="set_status" style="width:100px;">
        <option value="0" selected="selected">Not Started</option>
        <option value="1">Inprocess</option>
        <option value="2">Waiting on others</option>
        <option value="3">Deferred till later</option>
        <option value="9">Deleted</option>
      </select>        </td>
      <td>Priority:</td>
      <td><select name="set_priority" id="set_priority" style="width:100px;">
        <option value="0">Low</option>
        <option value="1" selected="selected">Normal</option>
        <option value="2">High</option>
        <option value="3">Critical</option>
      </select></td>
    </tr>
    <tr>
      <td>Project:</td>
      <td><select name="ProjectID" id="ProjectID">
          <?php foreach($projects as $project){ ?>
          <option value="<?php echo $project['ProjectID'];?>">
          <?php echo $project['project_label'];?>
          </option>
          <?php } ?>
        </select>      </td>
          <td>Task Type: </td>
          <td><select id="set_type" name="set_type">
              <option value="0">Bug</option>
              <option value="1">Idea</option>
              <option value="2">To-Do</option>
              <option value="3">Update</option>
              </select></td>
      <td>User:</td>
      <td><select name="UserID" id="UserID" style="width:100px;">
          <option>Select</option>
          <?php foreach ($users as $user){ ?>
          <option value="<?php echo $user['UserID'];?>">
          <?php echo $user['name_display'];?>
          </option>
          <?php } ?>
      </select></td>
    </tr>


    <tr>
      <td colspan="6"><textarea name="Body" id="Body" cols="60" rows="10"></textarea></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="formVNT" />
</form>